package com.mutouren.web.org;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.model.App;
import com.mutouren.modules.org.model.Function;
import com.mutouren.modules.org.service.AppService;
import com.mutouren.modules.org.service.FunctionService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/org/function")
public class FunctionAction {
	
	@Resource
	private AppService appService;
	
	@Resource
	private FunctionService functionService;
		
	@RequestMapping(value="/manage.action")
	public String manage(@RequestParam(value="appId", required=false, defaultValue="0") int appId,
			HttpServletRequest request) {
		
		request.setAttribute("appId", appId);
		if (appId != 0) {
			App app = appService.get(appId);
			request.setAttribute("appName", app.getAppName());
		}
		
		List<Function> listFunction = functionService.selectByAppId(appId, true);
		request.setAttribute("listFunction", OrgUtils.convertFunctionToJSON(listFunction));
		
		return "modules/org/functionList";
	}
	
	@RequestMapping(value="/add.action")
	public String add(@ModelAttribute("function") Function function, 
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			Function parent = functionService.get(function.getParentId());
			function.setParentFuncName(parent.getFuncName());
			if (parent.isRoot()) {
				function.setParentFuncName("");
			}			
			return "modules/org/functionForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, function, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		functionService.add(function);
		return WebUtils.successDialog(response);
	}
	
	@RequestMapping(value="/modify.action")
	public String modify(@ModelAttribute("function") Function function, 
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {	
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {						
			function = functionService.get(function.getFuncId());						
			Function parent = functionService.get(function.getParentId());
			function.setParentFuncName(parent.getFuncName());
			if (parent.isRoot()) {
				function.setParentFuncName("");
			}				
			
			map.addAttribute("function", function);
			return "modules/org/functionForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, function, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		int result = functionService.modify(function);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}
	}
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="funcId") int funcId, String validState,
			HttpServletResponse response) throws IOException {
		Function function = new Function();
		function.setFuncId(funcId);
		function.setValidState(ValidState.valueOf(validState));		
		
		if (functionService.setState(function) >= 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}	
	
	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// organization
		if (actionName.equals("add") || actionName.equals("modify")) {
			Function function = (Function) model;
			if (StringUtils.isBlank(function.getFuncName())) {
				errors.add("名称: 不能为空");
			}

			if (function.getLayoutCode() == null) {
				String layoutCode = request.getParameter("layoutCode");
				if (StringUtils.isBlank(layoutCode)) {
					function.setLayoutCode(0);
				} else {
					errors.add("布局编号: 格式有误，必须为数字");
				}
			}
			
			if (function.getSequence() == null) {
				String sequence = request.getParameter("sequence");
				if (StringUtils.isBlank(sequence)) {
					function.setSequence(0);
				} else {
					errors.add("顺序: 格式有误，必须为数字");
				}
			}			
		}
		return errors.size() > 0;
	}	
	
}
